<?php

include "DBconn.php";

class StockMaster {

    public function updateStock($pro_serialnumber, $qty, $type) {
        $query = "select on_hand,in_order,on_sale,sold from stock_header where pro_serialnumber='$pro_serialnumber'";
        $s = mysql_query($query) or die(mysql_error());
        if ($row == mysql_fetch_object($s)) {
            $on_hand = (int) $row->on_hand;
            $in_order = (int) $row->in_order;
            $on_sale = (int) $row->on_sale;
            $sold = (int) $row->sold;
            if ($type == "IN") {
                $on_hand = $on_hand + $qty;
                $in_order = $in_order - $qty;
                $query = "update stock_header set on_hand='$on_hand',in_order='$in_order' where pro_serialnumber='$pro_serialnumber'";
            } else if ($type == "OUT") {
                $on_hand = $on_hand - $qty;
                $on_sale = $on_sale - $qty;
                $sold = $sold + $qty;
                $query = "update stock_header set on_hand='$on_hand',on_sale='$on_sale',sold='$sold' where pro_serialnumber='$pro_serialnumber'";
            }
        }
        $s = mysql_query($query) or die(mysql_error());
        return "true";
    }

    public function addNewTransaction($VObject) {
        $query = "insert into stock_trans_details(trans_serialnumber_fk,trans_type,trans_date,cre_userid_fk,cre_datetime,activestatus) values('$VObject->trans_serialnumber_fk','$VObject->trans_type','$VObject->trans_date','$VObject->cre_userid_fk','$VObject->cre_datetime',1)";
        //$query="insert into stock_trans_details(trans_serialnumber_fk,trans_type,trans_date,cre_userid_fk,cre_datetime,activestatus) values ('$VObject->trans_serialnumber_fk','$VObject->trans_type','$VObject->trans_date','$VObject->cre_userid_fk','$VObject->cre_datetime',1)";
        $s = mysql_query($query) or die(mysql_error());
        if ($s) {
            $last_id = mysql_insert_id();
            foreach ($VObject->pro_serial_number_fk as $r => $value) {
                $trans_pro_qty = mysql_real_escape_string($VObject->trans_pro_qty[$r]);
                $total_trans_qty = mysql_real_escape_string($VObject->total_trans_qty[$r]);
                $pro_status = mysql_real_escape_string($VObject->pro_status[$r]);
                $trans_status = mysql_real_escape_string($VObject->trans_status[$r]);
                $warehouse_serialnumber_fk = mysql_real_escape_string($VObject->warehouse_serialnumber_fk[$r]);
                $query2 = "insert into stock_trans_pro_details(header_serialnumber_fk,pro_serial_number_fk,trans_pro_qty,total_trans_qty,pro_status,trans_status,warehouse_serialnumber_fk,cre_userid_fk,cre_datetime,activestatus) values('$last_id','$value','$trans_pro_qty','$total_trans_qty','$pro_status','$trans_status','$warehouse_serialnumber_fk','$VObject->cre_userid_fk','$VObject->cre_datetime',1)";
                mysql_query($query2) or die(mysql_error());
            }
        }
        return "true";
    }

    public function editTransaction($VObject) {
        $query = "update stock_trans_details set trans_date='$VObject->trans_date',cre_userid_fk='$VObject->cre_userid_fk',cre_datetime='$VObject->cre_datetime' where trans_serialnumber_fk='$VObject->trans_serialnumber_fk' and trans_type='$VObject->trans_type' and activestatus=1";
        $s = mysql_query($query) or die(mysql_error());
        if ($s) {
            mysql_query("update stock_trans_pro_details set activestatus=0 where header_serialnumber_fk='$VObject->trans_serialnumber_fk'") or die(mysql_error());
            foreach ($VObject->pro_serial_number_fk as $r => $value) {
                $trans_pro_qty = mysql_real_escape_string($VObject->trans_pro_qty[$r]);
                $total_trans_qty = mysql_real_escape_string($VObject->total_trans_qty[$r]);
                $pro_status = mysql_real_escape_string($VObject->pro_status[$r]);
                $trans_status = mysql_real_escape_string($VObject->trans_status[$r]);
                $warehouse_serialnumber_fk = mysql_real_escape_string($VObject->warehouse_serialnumber_fk[$r]);
                $query2 = "insert into stock_trans_pro_details(header_serialnumber_fk,pro_serial_number_fk,trans_pro_qty,total_trans_qty,pro_status,trans_status,warehouse_serialnumber_fk,cre_userid_fk,cre_datetime,activestatus) values('$VObject->trans_serialnumber_fk','$value','$trans_pro_qty','$total_trans_qty','$pro_status','$trans_status','$warehouse_serialnumber_fk','$VObject->cre_userid_fk','$VObject->cre_datetime',1)";
                mysql_query($query2) or die(mysql_error());
            }
        }
        return "true";
    }

    public function deleteTransaction($trans_serialnumber_fk) {
        $query = "update stock_trans_details set activestatus=0 where trans_serialnumber_fk='$trans_serialnumber_fk'";
        $s = mysql_query($query) or die(mysql_error());
        mysql_query("update stock_trans_pro_details set activestatus=0 where header_serialnumber_fk='$trans_serialnumber_fk'") or die(mysql_error());
        return "true";
    }

    public function getAllTransactions() {
        $ret = array();
        $query = "select * from stock_trans_details where activestatus=1";
        $s = mysql_query($query) or die(mysql_error());
        while ($row = mysql_fetch_object($s)) {
            $temp = new VoTrasactionMaster();
            $temp->serialnumber = (int) $row->serialnumber;
            $temp->trans_serialnumber_fk = (int) $row->trans_serialnumber_fk;
            $temp->trans_type = $row->trans_type;
            $temp->trans_date = $row->trans_date;
            $temp->cre_userid_fk = (int) $row->cre_userid_fk;
            $temp->cre_datetime = $row->cre_datetime;
            $temp->activestatus = (boolean) $row->activestatus;
            $query2 = "select * from stock_trans_pro_details where header_serialnumber_fk='$row->serialnumber' and activestatus=1";
            $s2 = mysql_query($query2) or die(mysql_error());
            while ($row2 = mysql_fetch_object($s2)) {
                $temp->detserialnumber[] = (int) $row2->detserialnumber;
                $temp->header_serialnumber_fk[] = (int) $row2->header_serialnumber_fk;
                $temp->pro_serial_number_fk[] = (int) $row2->pro_serial_number_fk;
                $temp->trans_pro_qty[] = (int) $row2->trans_pro_qty;
                $temp->total_trans_qty[] = (int) $row2->total_trans_qty;
                $temp->pro_status[] = $row2->pro_status;
                $temp->trans_status[] = $row2->trans_status;
                $temp->warehouse_serialnumber_fk[] = (int) $row2->warehouse_serialnumber_fk;
            }
            $ret[] = $temp;
            mysql_free_result($s2);
        }
        mysql_free_result($s);
        return $ret;
    }

}

class VoTrasactionMaster {

    var $serialnumber;
    var $trans_serialnumber_fk;
    var $trans_type;
    var $trans_date;
    var $cre_userid_fk;
    var $cre_datetime;
    var $activestatus;
    var $detserialnumber;
    var $header_serialnumber_fk;
    var $pro_serial_number_fk;
    var $trans_pro_qty;
    var $total_trans_qty;
    var $pro_status;
    var $trans_status;
    var $warehouse_serialnumber_fk;

}

/*
  $vo=new VoTrasactionMaster();
  $vo->trans_serialnumber_fk=1;
  $vo->trans_type=1;
  $vo->trans_date=1;
  $vo->cre_userid_fk=1;
  $vo->activestatus=1;
  $arr=array();
  $arr[0]=1;
  $rr=array();
  $rr[0]=1;
  $ar=array();
  $ar[0]=1;
  $vo->header_serialnumber_fk=$rr;
  $vo->pro_serial_number_fk=$arr;
  $vo->warehouse_serialnumber_fk=$ar;
  $ss=new StockMaster();
  echo $ss->addNewTransaction($vo); */
?>